package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.dto.RequestParams;
import com.hmall.common.pojo.Item;
import com.hmall.common.pojo.ItemDoc;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 分页查
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> list(int page, int size){
        Page<Item> pageInfo = new Page<>(page, size);
        itemService.page(pageInfo);
        long total = pageInfo.getTotal();
        List<Item> list = pageInfo.getRecords();
        return new PageDTO<>(total,list);
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
        return itemService.getById(id);
    }

    /**
     * 新增商品
     * @param item
     */
    @PostMapping
    public void save(@RequestBody Item item){
        /*item.setCreateTime(new Date());
        item.setUpdateTime(new Date());*/
        itemService.save(item);
        rabbitTemplate.convertAndSend("hotel.topic","hotel.insert",item.getId());
    }

    /**
     * 商品上下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable Long id,@PathVariable int status){
        Item item = itemService.getById(id);
        item.setStatus(status);
        itemService.updateById(item);
        //发消息
        if (status==1){
            rabbitTemplate.convertAndSend("hotel.topic","hotel.insert",id);
        }else if (status == 2){
            rabbitTemplate.convertAndSend("hotel.topic","hotel.delete",id);
        }
    }

    /**
     * 修改商品
     * @param item
     */
    @PutMapping
    public void update(@RequestBody Item item){
        itemService.updateById(item);
        rabbitTemplate.convertAndSend("hotel.topic","hotel.insert",item.getId());
    }

    /**
     * 根据id删除商品
     * @param id
     */
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
        itemService.removeById(id);
        rabbitTemplate.convertAndSend("hotel.topic","hotel.delete",id);
    }

    /**
     * 自动补全
     * @param key
     * @return
     */
    /*@GetMapping("/suggestion")
    public List<String> suggestion(String key){
        return itemService.suggestion(key);
    }

    *//**
     * 过滤项聚合
     * @param dto
     * @return
     *//*
    @PostMapping("/filters")
    public Map<String, List<String>> filters(@RequestBody RequestParams dto){
        return itemService.filters(dto);
    }

    *//**
     * 搜索
     *//*
    @PostMapping("/list2")
    public PageDTO<ItemDoc> list(@RequestBody RequestParams dto){
        return itemService.list(dto);
    }*/

    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable Long itemId,@PathVariable Integer num){
        Item item = itemService.getById(itemId);
        Integer stock = item.getStock();
        if (num!=null&&stock>=num) {
            item.setStock(stock - num);
            itemService.updateById(item);
            rabbitTemplate.convertAndSend("hotel.topic","hotel.insert",item.getId());
        }else {
            throw new RuntimeException("库存不足");
        }
    }
}
